package com.tsingteng.cosfun.ui.cosfun.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.pili.pldroid.player.widget.PLVideoTextureView;
import com.pili.pldroid.player.widget.PLVideoView;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.AwkwardBean;
import com.tsingteng.cosfun.helper.DialogHelper;
import com.tsingteng.cosfun.ui.cosfun.cosinterface.ObserverListener;
import com.tsingteng.cosfun.ui.cosfun.cosinterface.ObserverManager;

import java.util.ArrayList;
import java.util.List;

public class AwkwardAdapter extends RecyclerView.Adapter<AwkwardAdapter.ViewHolder> {
    Activity mContext = null;
    List<AwkwardBean.Awkward> videoList = null;
    private final Dialog loadingDialog;
    OnItemClickLisenter onItemClickLisenter = null;
    private boolean sharkFlag = true;

    public AwkwardAdapter(Activity mContext, List<AwkwardBean.Awkward> videoList, OnItemClickLisenter onItemClickLisenter) {
        this.mContext = mContext;
        this.videoList = videoList;
        loadingDialog = DialogHelper.getLoadingDialog(mContext);
        this.onItemClickLisenter = onItemClickLisenter;
    }


    @Override
    public AwkwardAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.awkward_item_view, parent, false);
        return new AwkwardAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(AwkwardAdapter.ViewHolder holder, int position) {
        holder.videoMian.setDisplayAspectRatio(PLVideoView.ASPECT_RATIO_FIT_PARENT);
        holder.videoMian.setVideoPath(videoList.get(position).getVideoUrl());
        Glide.with(mContext).load(videoList.get(position).getVideoCverUrl()).into(holder.video_cover);
        holder.videoMian.setCoverView(holder.video_cover);
        if (position == 0) {
//            holder.videoMian.start();
//            holder.videoMian.setLooping(true);
            if (onItemClickLisenter != null) {
                onItemClickLisenter.playFirst(position, holder.videoMian, null);
            }
        }
    }

    @Override
    public int getItemCount() {
        if (videoList == null) {
            return 0;
        }
        return videoList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView video_cover;
        PLVideoTextureView videoMian;
        ImageView img_play;
        RelativeLayout rootView;

        public ViewHolder(View itemView) {
            super(itemView);
            video_cover = itemView.findViewById(R.id.awkward_item_view_video_cover);
            videoMian = itemView.findViewById(R.id.awkward_item_view_video_main);
            img_play = itemView.findViewById(R.id.awkward_item_view_play);
            rootView = itemView.findViewById(R.id.awkward_item_view_root_view);
        }
    }

    public interface OnItemClickLisenter {
        void playFirst(int position, PLVideoTextureView p1, PLVideoTextureView p2);
    }

    public void setSharkFlag(boolean sharkFlag) {
        this.sharkFlag = sharkFlag;
    }
}
